package com.liukai.bishe.presenter.transactionPresenter.impl;

import com.alibaba.fastjson.JSON;
import com.liukai.bishe.model.API;
import com.liukai.bishe.model.doman.transaction.Transaction;
import com.liukai.bishe.model.doman.transaction.TransactionMessageList;
import com.liukai.bishe.model.doman.transaction.TransactionRankList;
import com.liukai.bishe.model.doman.transaction.TransactionTotal;
import com.liukai.bishe.model.doman.transaction.TransactionUseTime;
import com.liukai.bishe.model.doman.transaction.TransactionUserTimeMessageList;
import com.liukai.bishe.presenter.transactionPresenter.ITransactionPresenter;
import com.liukai.bishe.utils.Constants;
import com.liukai.bishe.utils.RetrofitManager;
import com.liukai.bishe.utils.ToastUtil;
import com.liukai.bishe.view.IBaseCallBack;
import com.liukai.bishe.view.ITransactionCallBack;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class ITransactionPresenterImpl implements ITransactionPresenter {

    private ITransactionCallBack mITransactionCallBack = null;

    @Override
    public void startTransaction(TransactionUseTime transactionUseTime) {

        String transactionUseTimeString = JSON.toJSONString(transactionUseTime);
        Retrofit retrofit = RetrofitManager.getInstance().getRetrofit();
        API api = retrofit.create(API.class);
        Call<List<TransactionUseTime>> task = api.downTransaction(transactionUseTime);
        task.enqueue(new Callback<List<TransactionUseTime>>() {
            @Override
            public void onResponse(Call<List<TransactionUseTime>> call, Response<List<TransactionUseTime>> response) {
                int code = response.code();
                if (code == HttpURLConnection.HTTP_OK){
                    List<TransactionUseTime> transactionUseTimeList = response.body();
                    ToastUtil.log("startTransaction","transactionUserTimeMessageList=============>",response.body().toString());
                    mITransactionCallBack.resultTrsaction(transactionUseTimeList);
                }
                ToastUtil.log("startTransaction","code=============>",code+"");
            }

            @Override
            public void onFailure(Call<List<TransactionUseTime>> call, Throwable t) {
                ToastUtil.log("startTransaction","code=============>",t.toString());
            }
        });

    }

    /**
     * 新增事务
     * @param transaction
     */
    @Override
    public void addTransaction(Transaction transaction) {

        String transactionString = JSON.toJSONString(transaction);
        Retrofit retrofit = RetrofitManager.getInstance().getRetrofit();
        API api = retrofit.create(API.class);
        Call<TransactionMessageList> addTransactionTask = api.addTransaction(transactionString,
                Constants.getUserInfo().getUsername());
        addTransactionTask.enqueue(new Callback<TransactionMessageList>() {
            @Override
            public void onResponse(Call<TransactionMessageList> call, Response<TransactionMessageList> response) {

                ToastUtil.log("ITransactionPresenterImpl",
                        "addTransaction",response.body().getTransactionList().toString());
                if (response.code() == HttpURLConnection.HTTP_OK){
                    TransactionMessageList transactionMessageList = response.body();
                    mITransactionCallBack.resultTrsaction(transactionMessageList);
                }

            }

            @Override
            public void onFailure(Call<TransactionMessageList> call, Throwable t) {
                ToastUtil.log("startTransaction","code=============>",t.toString());
            }
        });

    }

    /**
     *根据用户名查找该用户每一项任务总的执行情况
     * @param username
     */
    @Override
    public void findTransactionTotal(String username) {

        Retrofit retrofit = RetrofitManager.getInstance().getRetrofit();
        API api = retrofit.create(API.class);
        Call<List<TransactionTotal>> task = api.findTransactionTotalByUserName(username);
        task.enqueue(new Callback<List<TransactionTotal>>() {
            @Override
            public void onResponse(Call<List<TransactionTotal>> call, Response<List<TransactionTotal>> response) {
                int code = response.code();
                if (code == HttpURLConnection.HTTP_OK){
                    List<TransactionTotal> transactionTotalList = response.body();
                    ToastUtil.log("findTransactionTotal","transactionTotalList=============>",response.body().toString());
                    mITransactionCallBack.resultTransactionTotal(transactionTotalList);
                }
                ToastUtil.log("startTransaction","code=============>",code+"");
            }

            @Override
            public void onFailure(Call<List<TransactionTotal>> call, Throwable t) {
                ToastUtil.log("startTransaction","code=============>",t.toString());
            }
        });

    }

    /**
     * 删除事务
     * @param id
     */
    @Override
    public void deleteTransactionById(String id,String username) {

        Retrofit retrofit = RetrofitManager.getInstance().getRetrofit();
        API api = retrofit.create(API.class);
        Call<List<Transaction>> task = api.deleteTransactionById(id,username);
        task.enqueue(new Callback<List<Transaction>>() {
            @Override
            public void onResponse(Call<List<Transaction>> call, Response<List<Transaction>> response) {
                int code = response.code();
                if (code == HttpURLConnection.HTTP_OK){
                    List<Transaction> transactionList = response.body();
                    TransactionMessageList transactionMessageList = new TransactionMessageList();
                    transactionMessageList.setTransactionList(transactionList);
                    ToastUtil.log("findTransactionTotal","transactionTotalList=============>",response.body().toString());
                    mITransactionCallBack.resultTrsaction(transactionMessageList);
                }
                ToastUtil.log("startTransaction","code=============>",code+"");
            }

            @Override
            public void onFailure(Call<List<Transaction>> call, Throwable t) {
                ToastUtil.log("startTransaction","code=============>",t.toString());
            }
        });

    }

    /**
     * 修改事务信息
     * @param transaction
     */
    @Override
    public void updateTransactionById(Transaction transaction) {

        Retrofit retrofit = RetrofitManager.getInstance().getRetrofit();
        API api = retrofit.create(API.class);
        Call<List<Transaction>> task = api.updateTransactionById(transaction);
        task.enqueue(new Callback<List<Transaction>>() {
            @Override
            public void onResponse(Call<List<Transaction>> call, Response<List<Transaction>> response) {
                int code = response.code();
                if (code == HttpURLConnection.HTTP_OK){
                    List<Transaction> transactionList = response.body();
                    TransactionMessageList transactionMessageList = new TransactionMessageList();
                    transactionMessageList.setTransactionList(transactionList);
                    ToastUtil.log("findTransactionTotal","transactionTotalList=============>",response.body().toString());
                    mITransactionCallBack.resultTrsaction(transactionMessageList);
                }
                ToastUtil.log("startTransaction","code=============>",code+"");
            }

            @Override
            public void onFailure(Call<List<Transaction>> call, Throwable t) {
                ToastUtil.log("startTransaction","code=============>",t.toString());
            }
        });

    }

    @Override
    public void getRankList() {

        Retrofit retrofit = RetrofitManager.getInstance().getRetrofit();
        API api = retrofit.create(API.class);
        Call<List<TransactionRankList>> task = api.getRankList();
        task.enqueue(new Callback<List<TransactionRankList>>() {
            @Override
            public void onResponse(Call<List<TransactionRankList>> call, Response<List<TransactionRankList>> response) {
                int code = response.code();
                ToastUtil.log("getRankList","code--------->",code+"");
                List<TransactionRankList> transactionRankListList = response.body();
//                ToastUtil.log("getRankList","body--------->",response.body().toString());
                int i = 0;
                for (TransactionRankList transactionRankList : transactionRankListList) {

                    ToastUtil.log("getRankList","body--------->" + (++i),transactionRankList.toString());

                }
                mITransactionCallBack.result(response.body());

            }

            @Override
            public void onFailure(Call<List<TransactionRankList>> call, Throwable t) {

            }
        });

    }


    @Override
    public void registerCallback(IBaseCallBack iBaseCallBack) {
        this.mITransactionCallBack = (ITransactionCallBack)iBaseCallBack;
    }

    @Override
    public void unregisterCallback(IBaseCallBack iBaseCallBack) {
        mITransactionCallBack = null;
    }




}
